package band.wukong.practice.java.demo.sqlplus;

import band.wukong.practice.util.mess.JdbcUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Description: sql命令行工具
 * Author: jsun
 * Date: 2009-11-8 11:27:57
 */
public class SqlPlus {
    private static final String DB_DRIVER_MYSQL = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/test";
    private static final String NAME = "root";
    private static final String PWD = "ppp";

    public static void main(String[] args) {

        InputStream is = null;
        BufferedReader br = null;
        while (true) {
            printWelcomeInfo();
            System.out.print("Choose>");

            try {

                is = System.in;
                br = new BufferedReader(new InputStreamReader(is));

                String str = br.readLine();

                if (str.equals("exit")) {
                    System.out.println("GoodBye!");
                    break;
                } else if (str.equals("login")) {
                    System.out.println();
                    talk();
                }
            } catch (IOException e) {
                e.printStackTrace();
                break;
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }

        }
    }

    public static void talk() {
        Connection con = null;
        con = JdbcUtils.getConnection(URL, NAME, PWD, DB_DRIVER_MYSQL);
        System.out.println("Connection has been built.");

        InputStream is = System.in;
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader bbr = new BufferedReader(isr);

        PreparedStatement ps = null;
        ResultSet rs = null;
        while (true) {
            String sql = null;
            try {
                System.out.print("SQL>");
                sql = bbr.readLine();
                System.out.println();
                if (sql.equals("exit")) {
                    break;
                }
                ps = con.prepareStatement(sql);
                if (ps.execute()) {
                    rs = ps.executeQuery();
                    JdbcUtils.printResultSet(rs);
                } else {
                    ps.executeUpdate();
                    con.commit();
                }
            } catch (Exception e) {
                try {
                    con.rollback();
                } catch (SQLException e1) {
                    throw new RuntimeException(e1);
                }
            }

            JdbcUtils.closePreparedStatementAndResultSet(ps, rs);
            JdbcUtils.closeConnection(con);
        }
    }

    private static void printWelcomeInfo() {
        System.out.println("==============================");
        System.out.println("	Welcome to SQLPlUS!");
        System.out.println("==============================");
        System.out.println("Please choose one :");
        System.out.println("1.	Type \"login\"  to enter sqlplus.");
        System.out.println("0.	Type \"exit\" to leave.");
    }
}
